package com.tingyuge.thinking.spring.configuration.bean.lifecycle;

import com.tingyuge.thinking.spring.ioc.overview.domain.SuperUser;
import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

/**
 * MergeBeanDefinition 合并
 *
 * @author xiaodao
 * @version 1.0
 * @date 2021/7/10
 */
public class MergeBeanDefinitionDemo {

    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //基于 XML 资源 BeanDefinitionReader
        String location = "/META-INF/dependency-lookup-context.xml";
        //指定字符编码
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        Resource resource = new ClassPathResource(location);
        EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
        int beanDefinitions = xmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
        User user = beanFactory.getBean("user", User.class);
        System.out.println(user);
        User superUser = beanFactory.getBean("superUser", SuperUser.class);
        System.out.println(superUser);
    }
}
